import superService from "./superService";
import MiniApp from "./miniGameContent/miniApp";
import WxApi from "./miniGameContent/wx/wxApi";
import H5Api from "./miniGameContent/h5/h5Api";
import TtApi from "./miniGameContent/tt/ttApi";
import NativeApi from "./miniGameContent/native/native";
import ksApi from "./miniGameContent/ks/KSAPI";
import VivoApi from "./miniGameContent/vivo/vivoApi";
import OppoApi from "./miniGameContent/oppo/oppoApi";

export default class MiniGameService extends superService {

    protected className: string = '游戏平台服务';

    private static m_MiniAppClass: typeof MiniApp | any = null;
    private m_miniApp: MiniApp = null;

    constructor() {
        super();
        //平台
        switch (cc.sys.platform) {
            case cc.sys.WECHAT_GAME_SUB:
            case cc.sys.WECHAT_GAME: {
                // MiniGameService.m_MiniAppClass = WxApi;
                MiniGameService.m_MiniAppClass = ksApi;
                break;
            }
            case cc.sys.BYTEDANCE_GAME:
            case cc.sys.BYTEDANCE_GAME_SUB: {
                MiniGameService.m_MiniAppClass = TtApi;
                break;
            }
            case cc.sys.VIVO_GAME: {
                MiniGameService.m_MiniAppClass = VivoApi;
                break;
            }
            case cc.sys.OPPO_GAME: {
                MiniGameService.m_MiniAppClass = OppoApi;
                break;
            }
            default: {
                if (cc.sys.isNative) {
                    MiniGameService.m_MiniAppClass = NativeApi;
                } else {
                    if (cc.sys.platform == 3 || cc.sys.platform == 4) {
                        MiniGameService.m_MiniAppClass = ksApi;
                    } else {
                        MiniGameService.m_MiniAppClass = H5Api;
                    }
                }
                break;
            }
        }
    }

    async loadService(): Promise<void> {
        await super.loadService();
        this.m_miniApp = <MiniApp>new MiniGameService.m_MiniAppClass();
        //验证授权
        this.m_miniApp.validAppId();
        //预先加载
        this.m_miniApp.preCreateUnits();
        //
        return Promise.resolve();
    }

    getAppApiInstance() {
        return this.m_miniApp;
    }

    static getMiniGameAppStatic(): typeof MiniApp {
        return MiniGameService.m_MiniAppClass;
    }
}